package com.devops.jarmonitor.config;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Gauge;
import io.micrometer.core.instrument.MeterRegistry;
import io.micrometer.core.instrument.Timer;
import lombok.Getter;
import org.springframework.stereotype.Component;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * Prometheus自定义指标管理器
 */
@Component
@Getter
public class MetricsManager {

    private final MeterRegistry meterRegistry;
    
    // 业务指标
    private final Counter orderSuccessCounter;
    private final Counter orderFailCounter;
    private final Counter paymentSuccessCounter;
    private final Counter paymentFailCounter;
    private final Counter loginSuccessCounter;
    private final Counter loginFailCounter;
    
    // 系统指标
    private final AtomicInteger activeUsers = new AtomicInteger(0);
    private final AtomicInteger queueSize = new AtomicInteger(0);
    private final AtomicInteger dbConnections = new AtomicInteger(0);
    
    // 响应时间
    private final Timer apiResponseTimer;
    private final Timer dbQueryTimer;
    
    public MetricsManager(MeterRegistry meterRegistry) {
        this.meterRegistry = meterRegistry;
        
        // 初始化业务计数器
        this.orderSuccessCounter = Counter.builder("business_order_success_total")
                .description("订单成功总数")
                .tag("type", "order")
                .register(meterRegistry);
                
        this.orderFailCounter = Counter.builder("business_order_fail_total")
                .description("订单失败总数")
                .tag("type", "order")
                .register(meterRegistry);
                
        this.paymentSuccessCounter = Counter.builder("business_payment_success_total")
                .description("支付成功总数")
                .tag("type", "payment")
                .register(meterRegistry);
                
        this.paymentFailCounter = Counter.builder("business_payment_fail_total")
                .description("支付失败总数")
                .tag("type", "payment")
                .register(meterRegistry);
                
        this.loginSuccessCounter = Counter.builder("business_login_success_total")
                .description("登录成功总数")
                .tag("type", "login")
                .register(meterRegistry);
                
        this.loginFailCounter = Counter.builder("business_login_fail_total")
                .description("登录失败总数")
                .tag("type", "login")
                .register(meterRegistry);
        
        // 初始化Gauge指标
        Gauge.builder("business_active_users", activeUsers, AtomicInteger::get)
                .description("当前活跃用户数")
                .register(meterRegistry);
                
        Gauge.builder("business_queue_size", queueSize, AtomicInteger::get)
                .description("消息队列大小")
                .register(meterRegistry);
                
        Gauge.builder("business_db_connections", dbConnections, AtomicInteger::get)
                .description("数据库连接数")
                .register(meterRegistry);
        
        // 初始化Timer
        this.apiResponseTimer = Timer.builder("business_api_response_time")
                .description("API响应时间")
                .tag("type", "api")
                .register(meterRegistry);
                
        this.dbQueryTimer = Timer.builder("business_db_query_time")
                .description("数据库查询时间")
                .tag("type", "db")
                .register(meterRegistry);
    }
    
    // 增加订单成功
    public void incrementOrderSuccess() {
        orderSuccessCounter.increment();
    }
    
    // 增加订单失败
    public void incrementOrderFail() {
        orderFailCounter.increment();
    }
    
    // 增加支付成功
    public void incrementPaymentSuccess() {
        paymentSuccessCounter.increment();
    }
    
    // 增加支付失败
    public void incrementPaymentFail() {
        paymentFailCounter.increment();
    }
    
    // 增加登录成功
    public void incrementLoginSuccess() {
        loginSuccessCounter.increment();
    }
    
    // 增加登录失败
    public void incrementLoginFail() {
        loginFailCounter.increment();
    }
    
    // 设置活跃用户数
    public void setActiveUsers(int count) {
        activeUsers.set(count);
    }
    
    // 设置队列大小
    public void setQueueSize(int size) {
        queueSize.set(size);
    }
    
    // 设置数据库连接数
    public void setDbConnections(int count) {
        dbConnections.set(count);
    }
}
